package cn.easyutil.util.javaUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;

import org.yaml.snakeyaml.Yaml;

/**
 * 配置类文件
 * @author spc
 *
 */
public class ConfigFileUitl {
	
	private Map<String, Object> data = new HashMap<String, Object>();

	/**
	 * 读取propertites文件
	 * @param path
	 * @return
	 */
	public static ConfigFileUitl readPropertites(InputStream input){
		ConfigFileUitl util = new ConfigFileUitl();
		return util.loadPropertites(input);
	}
	/**
	 * 读取yaml文件
	 * @param input
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static ConfigFileUitl readYaml(InputStream input){
		Yaml yaml = new Yaml();
		Map<String,Object> map = yaml.load(input);
		ConfigFileUitl util = new ConfigFileUitl();
		Set<Entry<String, Object>> set = map.entrySet();
		for (Entry<String, Object> entry : set) {
			String key = entry.getKey();
			Object value = entry.getValue();
			if(value instanceof Map){
				util.exchange((Map)value, key);
			}else{
				util.data.put(key, value);
			}
		}
		
		return util;
	}
	
	/**
	 * 根据k获取v(允许*号匹配)
	 * @param key
	 * @return
	 */
	public Object getVal(String key){
		Set<Entry<String, Object>> entry = this.data.entrySet();
		for (Entry<String, Object> en : entry) {
			String k = en.getKey().toUpperCase();
			Object v = en.getValue();
			key = key.toUpperCase();
			if(!key.contains("*")){
				if(k.equals(key)){
					return v;
				}
				continue;
			}
			String[] split = key.split("\\*");
			boolean find = true;
			for (int i = 0; i < split.length; i++) {
				if(!k.contains(split[i])){
					find = false;
					break;
				}
				k = k.substring(k.indexOf(split[i]));
			}
			if(find){
				return v;
			}
		}
		return null;
	}
	
	/**
	 * 根据k获取多个v(允许*号匹配)
	 * @param key
	 * @return
	 */
	public Map<String, Object> getVals(String key){
		Map<String, Object> result = new HashMap<String, Object>();
		Set<Entry<String, Object>> entry = this.data.entrySet();
		for (Entry<String, Object> en : entry) {
			String k = en.getKey().toUpperCase();
			Object v = en.getValue();
			key = key.toUpperCase();
			if(!key.contains("*")){
				if(k.equals(key)){
					result.put(en.getKey(), v);
				}
				continue;
			}
			String[] split = key.split("\\*");
			boolean find = true;
			for (int i = 0; i < split.length; i++) {
				if(!k.contains(split[i])){
					find = false;
					break;
				}
				k = k.substring(k.indexOf(split[i]));
			}
			if(find){
				result.put(en.getKey(), v);
			}
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void exchange(Map<String,Object> map,String k){
		Set<Entry<String, Object>> set = map.entrySet();
		for (Entry<String, Object> entry : set) {
			String key = entry.getKey();
			if(k == null){
				k = key;
			}
			Object value = entry.getValue();
			if(value instanceof Map){
				exchange((Map)value,k+"."+key);
			}else{
				this.data.put(k+"."+key, value);
			}
		}
	}
	
	/**
	 * 读取propertites
	 * @param in
	 * @return
	 */
	public ConfigFileUitl loadPropertites(InputStream in){
		Properties properties = new Properties();
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(in));
			properties.load(bufferedReader);
			Iterator<Entry<Object, Object>> iterator = properties.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<Object, Object> next = iterator.next();
				String key = next.getKey().toString();
				this.data.put(key, next.getValue());
			}
			return this;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if(bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
		
	}
	
	public static void main(String[] args) {
		System.out.println(UUID.randomUUID().toString());
	}
}
